import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    creater: 'F76D052A-569A-48B9-A8E3-7E6D3CCEDFA4',
    url: '', // http://cpmobile.htbyoffice.com
    imgUrl: location.origin, // http://cpmobile.htbyoffice.com,
    title: '',
    endTime: '',
    tSet: '',
    questionList: [],
    appData: [],
    answerList: [],
    descList: [],
    titlePath: '',
    TakeInNumber: 0,
    letterList: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
    // 一下上发消息需要的字段
    commonList: [],
    SelectedEmployeeIds: [],
    SelectedEmployeeNames: [],
    SelectedShowEmployeeNames: [],
    SelectedShowEmployeeIds: [],
    message_desc: '',
    message_img: '',
    message_VoteId: ''
  },
  mutations: {
    updateState (state, option) {
      state[option.key] = option.value
    },
    reset_message (state) {
      state.commonList = []
      state.SelectedEmployeeIds = []
      state.SelectedEmployeeNames = []
      state.SelectedShowEmployeeNames = []
      state.SelectedShowEmployeeIds = []
      state.message_desc = ''
      state.message_img = ''
      state.message_VoteId = ''
    },
    reset (state) {
      state.id = ''
      state.title = ''
      state.descList = []
      state.endTime = ''
      state.tSet = ''
      state.titlePath = ''
      state.questionList = []
      state.answerList = []
      state.TakeInNumber = 0
    },
    init (state, data) {
      state.id = data.ID
      state.title = data.VoteTheme
      state.descList = data.VoteName ? JSON.parse(data.VoteName).descList : []
      state.endTime = data.EndDate
      state.tSet = data.RrsultShowType ? ['投票后'] : ['投票前']
      state.titlePath = data.FengMian
      state.TakeInNumber = data.TakeInNumber
      let questionList = []
      for (let i in data.votequestions) {
        let votequestion = data.votequestions[i]
        let answerList = []
        for (let j in votequestion.voteoptions) {
          let voteoption = votequestion.voteoptions[j]
          let answer = {
            title: voteoption.OptionName,
            img: voteoption.ImgList
          }
          answerList.splice(j, 0, answer)
        }
        let question = {
          type: votequestion.VoteQuestionType ? 'more' : 'one',
          MaxSelected: votequestion.MaxSelected,
          answerList: answerList,
          question: votequestion.VoteQuestionName,
          questionImg: votequestion.ImageUrl
        }
        questionList.splice(i, 0, question)
      }
      state.questionList = questionList
    }
  }
})

export default store
